using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Events;

public class StoryManager : MonoBehaviour
{
    public StoryConfig config;
    public TextMeshProUGUI[] textList;
    public Image[] bubbleList;
    public Image[] actorList;
    public int storyIndex;
    public UnityEvent storyEnd;

    private void Start()
    {
        ShowStory(0);
    }

    public void NextStory()
    {
        if(storyIndex + 1 >= config.storyList.Length)
        {
            storyEnd?.Invoke();
            Debug.Log("Story End");
            return;
        }
        storyIndex++;
        ShowStory(storyIndex);
    }

    void ShowStory(int storyIndex)
    {
        Story story = config.storyList[storyIndex];
        for(int i = 0; i < bubbleList.Length; i++)
        {
            bubbleList[i].gameObject.SetActive(i == story.actor);            
        }

        for(int i = 0; i < textList.Length; i++)
        {
            if(i == story.actor)
            {
                textList[i].text = story.talk;
            }
            else
            {
                textList[i].text = "";
            }
        }

        for(int i = 0; i < actorList.Length; i++)
        {
            if(i == story.actor)
            {
                actorList[i].sprite = story.actorImage;
            }
        }
    }
}
